package com.mijie.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.mijie.core.Paging;

public class PagerTag extends SimpleTagSupport{

	private String paging;
	
	@SuppressWarnings("unchecked")
	@Override
	public void doTag() throws JspException, IOException {
		PageContext pageContext = (PageContext) this.getJspContext();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		if(paging==null) return;
		Paging<Object> pagingObj = (Paging<Object>) request.getAttribute(paging);
		if(pagingObj!=null){
			JspWriter out = getJspContext().getOut();
			
			out.print("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">");
			out.print("<tfoot>");
			out.print("<tr>");
			out.print("<td align=\"left\">第"+pagingObj.getPageNum()+"页，共"+pagingObj.getPageCount()+"页，共"+pagingObj.getRows()+"条记录</td>");
			if(pagingObj.getPageCount()>1){
				out.print("<td align=\"right\">");
				if(pagingObj.getPageNum()==1) out.print("首页|"); else out.print("<a href=\"javascript:jumpPage(1)\">首页</a>|");
				
				if(pagingObj.hasPreviou()) out.print("<a href=\"javascript:jumpPage("+pagingObj.getPreviouNum()+")\">上一页</a>|");
				else out.print("上一页|");
				
				if(pagingObj.hasNext()) out.print("<a href=\"javascript:jumpPage("+pagingObj.getNextNum()+")\">下一页</a>|");
				else out.print("下一页|");
				
				if(pagingObj.getPageNum()==pagingObj.getPageCount() || pagingObj.getPageCount()==0) out.print("末页");
				else out.print("<a href=\"javascript:jumpPage("+pagingObj.getPageCount()+")\">末页</a>");
				
				out.print("转到第<input id=\"jumpPageNo\" type=\"text\" style=\"width: 40px;\"  /> 页&nbsp;");
				out.print("<input type=\"button\" value=\"跳转\" onclick=\"jumpPage($('#jumpPageNo').val(), "+pagingObj.getPageCount()+");\" />");
				out.print("</td>");
			}
			out.print("</tr>");
			out.print("</tfoot>");
			out.print("</table>");
			/*
			out.print("<ul class=\"paginator\">");
			out.print("<li>"+pagingObj.getRows()+"条记录,共"+pagingObj.getPageCount()+"页 &nbsp;&nbsp;</li>");
			if(pagingObj.getPageCount()>1){
				if(pagingObj.hasPreviou()) out.print("<li><a href=\"javascript:jumpPage("+pagingObj.getPreviouNum()+")\">上一页</a></li>");
				long min = pagingObj.getPageNum()-5;
				long max = pagingObj.getPageNum()+5;
				if(min<1) min = 1;
				if(max>pagingObj.getPageCount()) max = pagingObj.getPageCount();
				for(long i=min;i<max;i++){
					if(i==pagingObj.getPageNum()){
						out.print("<li class=\"current\"><a href=\"javascript:jumpPage("+i+")\">"+i+"</a></li>");
					}else{
						out.print("<li><a href=\"javascript:jumpPage("+i+")\">"+i+"</a></li>");
					}
				}
				if(pagingObj.hasNext()) out.print("<li><a href=\"javascript:jumpPage("+pagingObj.getNextNum()+")\">下一页</a></li>");
			}
			out.print("</ul>");*/
			out.print("<input type=\"hidden\" id=\"pageNum\"  name=\"pageNum\" id=\"pageNum\" value="+pagingObj.getPageNum()+" />");
		}
	}

	public void setPaging(String paging) {
		this.paging = paging;
	}
	
}
